﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO.Common;
using DTO.Headquarters;
using System.ServiceModel;
using ServiceInterfaces.Headquarters;
using System.Web.UI.HtmlControls;

namespace GUI.Headquarter
{
    public partial class Orders : System.Web.UI.Page
    {
        public static int PAGES_SHOWN = 5;
        public static int RECORDS_PER_PAGE = 10;

        protected ProductList productsToOrder;
        protected OrderStatus orderStatus;
        protected int currPage = 1;
        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            orderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), orderStatusSelect.SelectedValue);

            if (!string.IsNullOrEmpty(Request.QueryString["page"])){
                int.TryParse(Request.QueryString["page"], out currPage);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ChannelFactory<IHeadquartersManagement> factory = new ChannelFactory<IHeadquartersManagement>("headquarter");
            IHeadquartersManagement headquarter = factory.CreateChannel();
            productsToOrder = headquarter.GetProductsToOrder(currPage, RECORDS_PER_PAGE, orderStatus);
            factory.Close();

            SetPageProperties(PageControlTop, productsToOrder.Total);
            SetPageProperties(PageControlBottom, productsToOrder.Total);
        }

        private void SetPageProperties(GUI.PaginationControl.PaginationControl control, int totalResults) 
        {
            control.CurrentPage = currPage;
            control.ResultsPerPage = RECORDS_PER_PAGE;
            control.PagesShown = PAGES_SHOWN;
            control.TotalResults = totalResults;
        }
    }
}